//
//  XMFavoriteListVC.m
//  XMWebTool
//
//  Created by ext.zhangmingwei8 on 2023/12/7.
//

#import "XMFavoriteListVC.h"
#import "XMCustomNaviView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIColor+XMTool.h"
#import "XMSizeMacro.h"
#import "XMToast.h"
#import "UIView+XMTool.h"


@interface XMFavoriteListVC ()<UITextFieldDelegate>

@property (nonatomic, strong) XMCustomNaviView  *naviView;

@property (nonatomic, strong) UITextField   *textField;

@end

@implementation XMFavoriteListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.naviView = [XMCustomNaviView getInstanceWithTitle:@"收藏列表"];
    [self.naviView setRightBtnImage:nil title:@"确定"];
    __weak typeof(self) weakSelf = self;
    [self.naviView setRightBlock:^{
        [weakSelf submitAction];
    }];
    [self.view addSubview:self.naviView];
    self.view.backgroundColor = [UIColor getWhiteColor_1];

    [self initAllView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textField resignFirstResponder];
}

- (void)initAllView {
    [self.view addSubview:self.textField];
    self.textField.text = UIPasteboard.generalPasteboard.string;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

- (void)submitAction {
    NSString *urlString = self.textField.text;
    NSURL *url = [NSURL URLWithString:urlString];
    if ( !url || ![[UIApplication sharedApplication] canOpenURL:url]) {
        [XMToast showTextToCenter:@"请输入正确的网址"];
        return;
    }
    [self.textField resignFirstResponder];
    NSArray *localArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUrlArr"];
    NSMutableArray *currentArr = [NSMutableArray arrayWithArray:localArr];
    

    if ([currentArr containsObject:urlString]) {
        [currentArr removeObject:urlString];
        [XMToast showTextToCenter:@"移除成功"];
    } else {
        [currentArr addObject:urlString];
        [XMToast showTextToCenter:@"添加成功"];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:currentArr forKey:@"kUrlArr"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifi_reloadPage" object:nil];
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, kNaviStatusBarH_XM + 10, kScreenWidth_XM - 20, 66)];
        _textField.backgroundColor = [UIColor whiteColor];
        _textField.returnKeyType = UIReturnKeyGo;
        _textField.delegate = self;
        _textField.font = [UIFont boldSystemFontOfSize:15];
        _textField.textColor = [UIColor getBlackColor_1];
        _textField.placeholder = @"请输入网址";
        _textField.clearButtonMode = UITextFieldViewModeAlways;
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 5;
        UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 66)];
        _textField.leftView = leftV;
        _textField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _textField;
}

@end
